package com.syqc.monitor.ui.cost

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.luck.picture.lib.entity.LocalMedia
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.comlib.utils.GsonUtil
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil
import okhttp3.MultipartBody

class OilFeeViewModel : AutoDisposeViewModel() {
    private var isSuccess = false

    //日期
    var upDate = ""

    //车辆ID
    var carId = ""

    //照片下标
    var photoIndex = 0
    fun initData() {
        upDate = TimeUtil.instance.getNowTime(true)
    }

    val resultLive = MutableLiveData<Boolean>()

    ///************************************加油费****************************************

    private var oilFeeEntity: String = ""
    private var parts: ArrayList<MultipartBody.Part?>? = null
    fun upOilFee() {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        parts = getBodyPartsByPath()
        if (updateName.isNullOrEmpty()) {
            toast(R.string.toast_upload_over_one)
            return
        }
        val repairPhotos = GsonUtil.toJson(carPhotos)
        if ("$carId$upDate$repairPhotos" == oilFeeEntity) {
            toast(R.string.toast_repeat_same_data)
            return
        }

        oilFeeEntity = "$carId$upDate$repairPhotos"
        update()
    }

    /** 提交费用 */
    fun update() {
        toastLoading(true)
        ServiceHelper.getRepository()
            .updateOilFee(carId, upDate, updateName.toString(), parts)
            .autoDispose(this).subscribe({
                toast(R.string.toast_add_fee_success)
                isSuccess = true
                toastLoading(false)
                resultLive.postValue(true)
            }, {
                isSuccess = false
                toastLoading(false)
                it.message?.run { toast(this) }
                resultLive.postValue(false)
            })
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    //照片路径| 加油机照片 | imgOilBefore | 非必填项 |
    //| 仪表台照片 | imgOilAfter | 非必填项 |
    //| 票据照片 | imgTicket | 非必填项 |
    private val carPhotos = arrayListOf("", "", "")
    private val photoType = arrayListOf("\"imgOilBefore\"", "\"imgOilAfter\"", "\"imgTicket\"")
    private val updateName = arrayListOf<String>()
    val photoOilLive = MutableLiveData<ArrayList<String>>()

    /**
     * 移除图片
     *
     * @param position
     */
    fun removeIndexPhoto(position: Int) {
        photoIndex = position
        carPhotos[position] = ""
    }

    fun handlePhoto(medias: ArrayList<LocalMedia>?) {
        medias?.run {
            carPhotos[photoIndex] = this[0].path
            photoOilLive.postValue(carPhotos)
        }
    }

    private fun getBodyPartsByPath(): ArrayList<MultipartBody.Part?>? {
        updateName.clear()
        if (carPhotos.isEmpty()) return null
        val parts = ArrayList<MultipartBody.Part?>()
        for ((index, path) in carPhotos.withIndex()) {
            if (path.isNotEmpty()) {
                val name = photoType[index]
                updateName.add(name)
                parts.add(ToolsUtil.getPartByPath("files", "fee_pic_$index", path))
            }
        }
        return parts
    }

    fun clearData() {
        (0..<carPhotos.size).forEach {
            carPhotos[it] = ""
        }
        photoOilLive.postValue(carPhotos)
    }

    init {
        photoOilLive.postValue(carPhotos)
    }
}